/**
 * 菜单JS
 */
angular.module('entrance.module.homepage', [])

    .config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {
        $stateProvider
            .state('search', {
                url: '/search/:goodsName',
                templateUrl: ent_templateprefix + 'search/search.html',
                controller: 'SearchController'
            })
            //首页
            .state('botMenu.homepage', {
                url: '/',
                templateUrl: ent_templateprefix + 'home/homepage.html',
                controller: 'HomepageController'
            })
        ;
    }])

    .controller('HomepageController', ['$scope', '$state', 'EntAuthService', function ($scope, $state, EntAuthService) {
        EnterChange(1);
        EntAuthService.getOpenid();//初始化微信支付openid的初始值
        //$scope.code = EntAuthService.getstrogecode();
        //alert("controller_code:"+$scope.code);
        $scope.ent_mallurl = projectBaseUrl;
        $scope.ent_expurl = projectBaseUrl;
        $scope.homepage_hot = entrance_config.homepage_hot;
        $scope.userid = EntAuthService.getUserid();
        $scope.goods_list = EntAuthService.getGood_name();
        /*goTop();//返回顶部*/
        //slid("slider");
        //scoll_color();
        clickchange();
        clickback();
        $scope.alert_warning = function () {
            alert("该功能尚未开发！")
        }
        $scope.search = function (goodName) {
            $state.go("search", {goodsName: goodName});
        }
        $scope.history_remove = function () {
            EntAuthService.removeGood_name();
            $scope.goods_list = EntAuthService.getGood_name();
        }
        $scope.clickopen = function () {
            var len = $scope.searchGood.length;
            if (len > 0) {
                $(".seek-delete").show();
            } else {
                $(".seek-delete").hide();
            }
        }
        $scope.clickdelete = function () {
            $scope.searchGood = '';
            $(".seekbox").focus();
            $(".seek-delete").hide();
        }//轮播图片数组
        var sliderImgList = [
            /*{"action": "", "imgSrc": "images/lunbo1.png"},
            {"action": "", "imgSrc": "images/lunbo2.png"},*/
            {"action": "", "imgSrc": "images/lunbo3.png"},
            /*{"action": "", "imgSrc": "images/lunbo4.png"},*/
        ];
        var defaults = {
            loop: true,
            smallBtn: true,
            number: false,
            laseMoveFn: false,
            location: false,
            preDef: "lnr",
            autoPlay: true,
            autoHeight: true,
            preFn: null,
            lastImgSlider: false,
            playTime: 3000,
            callback: null,
            fullScreen: true,
            verticalCenter: false,
            total: 0
        }
        var sliderList = {
            dom: document.getElementById('slider'),
//        dom:$("#slider"),
            data: sliderImgList,
            defaults: defaults
        }
        new Slider(sliderList);
    }])
    .controller('SearchController', ['$scope', '$stateParams', 'EntAuthService', function ($scope, $stateParams, EntAuthService) {
        $scope.ent_mallurl = projectBaseUrl;
        search_top();
        $scope.searchGood = $stateParams.goodsName;
        var search = {
            num: 10,
            n: 1,
            q: $scope.searchGood,
            seq: "sale_num",
            up: "asc"
        }
        $scope.load = true;
        $scope.search = function () {
            search.q = $scope.searchGood;
            EntAuthService.searchGoods(search).$promise.then(function (data) {
                $scope.goodslist = [];
                if(data.length<10){
                    $scope.load = false;
                }else {
                    $scope.load = true;
                }
                for (var i = 0; i < data.length; i++) {
                    $scope.goodslist.push(data[i]);
                }
            })
        }
        //销量排序
        var before = $("#sale_num");
        var beforeText = '';
        var priceSort = $('#price_sort');
        var priceSortWay = priceSort.find('div');
        var SearchCover = $('#SearchCover');
        var selfText = '';
        var self = '';
        var priceSortValue = 'hide';
        var ngbSearchDody = document.getElementById('ngb_search_body')
        SearchCover.click(function(){
            priceSort.addClass('displayNone');
            SearchCover.addClass('displayNone');
            ngbSearchDody.removeEventListener('touchmove',searchCover, false);
            priceSortValue = 'hide';
        })
        function searchCover(e){
            e.preventDefault();
        }
        $('#searchSort16').find('li').each(function () {
            $(this).click(function () {
                if (this.id == 'price') {
                    if(priceSortValue == 'hide'){
                        priceSortValue = 'show';
                        var liId = this.id;
                        var beforeLiId = search.seq;
                        ngbSearchDody.addEventListener('touchmove',searchCover, false);
                        th = $(this);
                        priceSort.removeClass('displayNone');
                        SearchCover.removeClass('displayNone');
                        search.seq = liId;
                        if(self!=''){
                            priceSortWay.removeClass("active");
                            self.setAttribute("class","active");
                        }
                        for (var i = 0; i < priceSortWay.length; i++) {
                            priceSortWay[i].onclick = function () {
                                self = this;
                                selfText = this.innerText;
                                search.n = 1;
                                switch (selfText) {
                                    case '从高到低':
                                        if (beforeLiId == liId && beforeText == selfText) {
                                            priceSort.addClass('displayNone');
                                            SearchCover.addClass('displayNone');
                                        } else {
                                            search.up = 'desc';
                                            EntAuthService.searchGoods(search).$promise.then(function (data) {
                                                $scope.goodslist = [];
                                                if(data.length<10){
                                                    $scope.load = false;
                                                }else {
                                                    $scope.load = true;
                                                }
                                                for (var i = 0; i < data.length; i++) {
                                                    $scope.goodslist.push(data[i]);
                                                }
                                                if (beforeLiId != liId) {
                                                    before.removeClass("active")
                                                    th.addClass("active down");
                                                } else {
                                                    if (beforeText != selfText) {
                                                        before.removeClass("active up")
                                                        th.addClass("active down");
                                                    }
                                                }
                                                before = th;
                                                beforeText = selfText;
                                                priceSort.addClass('displayNone');
                                                SearchCover.addClass('displayNone');
                                                ngbSearchDody.removeEventListener('touchmove',searchCover, false);
                                                priceSortValue = 'hide';
                                            })
                                        }
                                        break;
                                    case '从低到高':
                                        if (beforeLiId == liId && beforeText == selfText) {
                                            priceSort.addClass('displayNone');
                                            SearchCover.addClass('displayNone');
                                        } else {
                                            search.up = 'asc';
                                            EntAuthService.searchGoods(search).$promise.then(function (data) {
                                                $scope.goodslist = [];
                                                if(data.length<10){
                                                    $scope.load = false;
                                                }else {
                                                    $scope.load = true;
                                                }
                                                for (var i = 0; i < data.length; i++) {
                                                    $scope.goodslist.push(data[i]);
                                                }
                                                if (beforeLiId != liId) {
                                                    before.removeClass("active")
                                                    th.addClass("active up");
                                                } else {
                                                    if (beforeText != selfText) {
                                                        before.removeClass("active down")
                                                        th.addClass("active up");
                                                    }
                                                }
                                                before = th;
                                                beforeText = selfText;
                                                priceSort.addClass('displayNone');
                                                SearchCover.addClass('displayNone');
                                                ngbSearchDody.removeEventListener('touchmove',searchCover, false);
                                                priceSortValue = 'hide';
                                            })
                                        }
                                        break;
                                    default:
                                        break;
                                }
                            };
                        }
                    }else {
                        priceSort.addClass('displayNone');
                        SearchCover.addClass('displayNone');
                        ngbSearchDody.removeEventListener('touchmove',searchCover, false);
                        priceSortValue = 'hide';
                    }
                }
                if (search.seq != this.id && this.id != 'price') {
                    search.seq = this.id;
                    var th = $(this)
                    search.n = 1;
                    search.up = 'asc';
                    EntAuthService.searchGoods(search).$promise.then(function (data) {
                        $scope.goodslist = [];
                        if(data.length<10){
                            $scope.load = false;
                        }else {
                            $scope.load = true;
                        }
                        for (var i = 0; i < data.length; i++) {
                            $scope.goodslist.push(data[i]);
                        }
                        before.removeClass("active")
                        th.addClass("active");
                        before = th;
                        priceSort.addClass('displayNone');
                        SearchCover.addClass('displayNone');
                        ngbSearchDody.removeEventListener('touchmove',searchCover, false);
                        priceSortValue = 'hide';
                    })
                }
            })
        })
        $scope.clickopen = function () {
            var len = $scope.searchGood.length;
            if (len > 0) {
                $(".seek-delete").show();
            } else {
                $(".seek-delete").hide();
            }
        }
        $scope.clickdelete = function () {
            $scope.searchGood = '';
            $(".seekbox").focus();
            $(".seek-delete").hide();
        }
        $scope.clickopen();
        $scope.search();
        $(window).scroll(function () {
            var scrollTop = $(this).scrollTop();
            var scrollHeight = $(document).height();
            var windowHeight = $(this).height();
            if (scrollTop + windowHeight >= scrollHeight - 50) {
                if(search.n==1){
                    search.n++
                }
                EntAuthService.searchGoods(search).$promise.then(function (data) {
                    search.n++
                    if(data.length<10){
                        $scope.load = false;
                    }else {
                        $scope.load = true;
                    }
                    for (var i = 0; i < data.length; i++) {
                        $scope.goodslist.push(data[i]);
                    }
                })
            }
        })
    }])
;
